<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:nwisdocs="http://wales.nhs.uk/nwis/docs"
	exclude-result-prefixes="xs xsi">

  <xsl:template name="gp-details" match="nwisdocs:PatientDetails/nwisdocs:GPdetails">   
    <div class="boxed-section" id="{local-name()}">
      <h2>GP details</h2>
      <xsl:choose>
        <xsl:when test="(count(nwisdocs:RegisteredGPName/*) &gt; 0) or (nwisdocs:RegisteredGPcode/nwisdocs:IdValue[text()!='']) or (nwisdocs:Practice/nwisdocs:OrganisationName[text()!=''])">
            <xsl:choose>
              <xsl:when test="count(nwisdocs:RegisteredGPName/*) &gt; 0">
                <label>GP Name</label>
                <span>
                <xsl:value-of select="nwisdocs:RegisteredGPName/nwisdocs:Title"/>
                <xsl:value-of select="nwisdocs:RegisteredGPName/nwisdocs:GivenName"/>
                <xsl:value-of select="nwisdocs:RegisteredGPName/nwisdocs:FamilyName"/>
                </span>
                <br />
              </xsl:when>
            </xsl:choose>
            <xsl:choose>
              <xsl:when test="nwisdocs:Practice/nwisdocs:OrganisationId/nwisdocs:IdValue[text()!='']">
                <label>Organisation ID</label>
                <span>
                  <xsl:value-of select="nwisdocs:Practice/nwisdocs:OrganisationId/nwisdocs:IdValue"/>
                </span>
                <br />
              </xsl:when>
            </xsl:choose>
            <xsl:choose>
              <xsl:when test="nwisdocs:Practice/nwisdocs:OrganisationName[text()!='']">
                <label>GP Address</label>
                <span>
                <xsl:value-of select="nwisdocs:Practice/nwisdocs:OrganisationName"/>
                <br />
                <xsl:for-each select="nwisdocs:Practice/nwisdocs:OrganisationAddress/nwisdocs:StructuredAddress/nwisdocs:AddressLine">
                  <xsl:if test=". != ''">
                    <xsl:value-of select="."/>
                    <br />
                  </xsl:if>
                </xsl:for-each>
                <xsl:value-of select="nwisdocs:Practice/nwisdocs:OrganisationAddress/nwisdocs:PostCode"/>
                </span>  
              </xsl:when>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <label>None recorded</label>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>
</xsl:stylesheet>
